type SwitchPropsType = {
  fallback?: JSX.Element, // 默认值
  strict?: boolean, // 是否严格模式
  children: any // 字节点
}
/**
 * 条件判断组件
 */
function Switch ({ fallback, strict = true, children }: SwitchPropsType) {

  const render = () => {
    const soureChildren = Array.isArray(children) ? children : [children]
    let renderChildren = null

    if (strict) { // 是：渲染第一个符合条件的字节点
      renderChildren = soureChildren.find((child: any) => child.type === Switch.Match && child.props?.when) || fallback
    } else { // 否：展示所有符合条件的字节点
      renderChildren = soureChildren.filter((child: any) => child.type === Switch.Match && child.props?.when)
    }

    return (
      <>{ strict ? renderChildren : (renderChildren.length ? renderChildren : fallback) }</>
    )
  }

  return (
    <>{ children ? render() : fallback }</>
  )
}

Switch.Match = (props: { when: boolean, children: any }) => (<>{ props.when && props.children }</>)

export default Switch
